import Vue from "vue";
import { Toast } from "vant";

Vue.use(Toast);
export default {
  data() {
    return {
      timer: null,
      code: null,
      NfcAdapter: null,
      _nfcAdapter: null,
      main: null,
      pendingIntent: null,
      IntentFilter: null,
      techListsArray: null,
    };
  },
  created() {
    this.$store.dispatch("SET_NFCKANBAN", null);
    let timer = setTimeout(() => {
      clearTimeout(timer);
      this.code = null;
      if (window.plus) {
        this.NFCInit();
      } else {
        // 兼容老版本的plusready事件
        document.addEventListener("plusready", this.NFCInit, false);
      }
    }, 1000);
  },
  beforeDestroy() {
    document.removeEventListener("plusready", this.NFCInit, false);
    document.removeEventListener("newintent", this.read, false);
    clearInterval(this.timer);
  },
  methods: {
    loadNewintent() {
    //   clearInterval(this.timer);
      this.code = null;
      this.$store.dispatch("SET_NFCKANBAN", this.code);
      this._nfcAdapter.enableForegroundDispatch(
        this.main,
        this.pendingIntent,
        this.IntentFilter,
        this.techListsArray
      );
    //   document.removeEventListener("newintent", this.read, false);
    //   let timer = setTimeout(() => {
    //     clearTimeout(timer);
    //     document.addEventListener("newintent", this.read, false);
    //   }, 2000);
    },
    read() {
      this.code = null;
      this.timer = setTimeout(this.NFCReadUID, 2000);
    },
    NFCInit() {
      try {
        var main = (this.main = plus.android.runtimeMainActivity());
        var Intent = plus.android.importClass("android.content.Intent");
        var Activity = plus.android.importClass("android.app.Activity");
        var PendingIntent = plus.android.importClass(
          "android.app.PendingIntent"
        );
        var IntentFilter = (this.IntentFilter = plus.android.importClass(
          "android.content.IntentFilter"
        ));
        this.NfcAdapter = plus.android.importClass("android.nfc.NfcAdapter");
        this._nfcAdapter = this.NfcAdapter.getDefaultAdapter(main);

        var ndef = new IntentFilter("android.nfc.action.NDEF_DISCOVERED"); //NfcAdapter.ACTION_NDEF_DISCOVERED
        var tag = new IntentFilter("android.nfc.action.TAG_DISCOVERED"); //NfcAdapter.ACTION_TECH_DISCOVERED
        var tech = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
        var intentFiltersArray = [ndef, tag, tech];

        var techListsArray = (this.techListsArray = [
          ["android.nfc.tech.Ndef"],
          ["android.nfc.tech.IsoDep"],
          ["android.nfc.tech.NfcA"],
          ["android.nfc.tech.NfcB"],
          ["android.nfc.tech.NfcF"],
          ["android.nfc.tech.Nfcf"],
          ["android.nfc.tech.NfcV"],
          ["android.nfc.tech.NdefFormatable"],
          ["android.nfc.tech.MifareClassi"],
          ["android.nfc.tech.MifareUltralight"],
        ]);

        var _intent = new Intent(main, main.getClass());
        _intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

        var pendingIntent = (this.pendingIntent = PendingIntent.getActivity(
          main,
          0,
          _intent,
          0
        ));
        console.log("pendingIntent");
        if (this._nfcAdapter == null) {
          Toast("本设备不支持NFC!");
        } else if (this._nfcAdapter.isEnabled() == false) {
          Toast("NFC功能未打开!");
        } else {
          document.addEventListener("newintent", this.read, false);
          this._nfcAdapter.enableForegroundDispatch(
            main,
            pendingIntent,
            IntentFilter,
            techListsArray
          );
        }
      } catch (e) {}
    },
    NFCReadUID() {
      if (this.code) {
        return;
      }
      var main = plus.android.runtimeMainActivity();
      var _intent = main.getIntent();
      var _action = _intent.getAction();
      if (
        this.NfcAdapter.ACTION_NDEF_DISCOVERED == _action ||
        this.NfcAdapter.ACTION_TAG_DISCOVERED == _action ||
        this.NfcAdapter.ACTION_TECH_DISCOVERED == _action
      ) {
        var uid = _intent.getByteArrayExtra(this.NfcAdapter.EXTRA_ID);
        this.code = this.hex2int(this.Bytes2HexString(uid));
        if (this.code) {
          this.$store.dispatch("SET_NFCKANBAN", this.code);
        }
      }
    },
    //将byte[] 转为Hex，
    Bytes2HexString(arrBytes) {
      var str = [];
      for (var i = arrBytes.length - 1; i >= 0; i--) {
        var tmp;
        var num = arrBytes[i];
        if (num < 0) {
          tmp = (255 + num + 1).toString(16);
        } else {
          tmp = num.toString(16);
        }
        if (tmp.length == 1) {
          tmp = "0" + tmp;
        }
        str += tmp;
      }
      return str;
    },
    hex2int(hex) {
      var len = hex.length,
        a = new Array(len),
        code;
      for (var i = 0; i < len; i++) {
        code = hex.charCodeAt(i);
        if (48 <= code && code < 58) {
          code -= 48;
        } else {
          code = (code & 0xdf) - 65 + 10;
        }
        a[i] = code;
      }
      return a.reduce(function(acc, c) {
        acc = 16 * acc + c;
        return acc;
      }, 0);
    },
  },
};
